<template>
	<div v-loading="loading">
		<h3>基础信息</h3>
		<AplidForm ref="formRef" v-bind="editFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
		<CourseArrange ref="theList" :headId="props.nowRowData.id" :signUpEndTime="theSignUpEndTime"></CourseArrange>
		<div class="btn_part">
			<el-button type="" @click="toUpdate"> 提交 </el-button>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted } from 'vue'
import { editFormConfig } from '../config'
import CourseArrange from './courseArrange.vue'
import apiMod from '@/apiMod/index'
import { cloneDeep } from 'lodash-es'

let emit = defineEmits(['refresh'])
let props = defineProps({
	nowRowData: {
		type: Object,
		default: () => ({ id: undefined }),
	},
	isRead: {
		type: Boolean,
		default: false,
	},
})
let loading = ref(false)
let data: any = ref({})

let formModel: any = ref({ signUpEndTime: null })
let theSignUpEndTime = ref('')
watch(
	() => formModel.value.signUpTime,
	(val) => {
		if (val && val.length > 0) {
			formModel.signUpEndTime = val[1]
			theSignUpEndTime.value = val[1]
		}
	},
	{ immediate: true, deep: true }
)
let id = ref('')
const getDetail = () => {
			loading.value = true
			data.value = props.nowRowData
			id.value = props.nowRowData.id
			formModel.value = {
				trainingName: data.value.trainingName || undefined,
				organizationName: data.value.organizationName || undefined,
				trainingType: data.value.trainingType || undefined,
				trainingTypeLabel: data.value.trainingTypeLabel || undefined,
				trainingUnitName: data.value.trainingUnitName || undefined,
				signUpTime: [data.value.signUpStartTime, data.value.signUpEndTime] || undefined,
				signUpEndTime: data.value.signUpEndTime || undefined,
				trainingLevel: data.value.trainingLevel || undefined,
				trainingLevelLabel: data.value.trainingLevelLabel || undefined,
				trainingIntroduction: data.value.trainingIntroduction || undefined,
			}
			loading.value = false
}
onMounted(()=>{
	getDetail()
})

let courseBaseConfig = ref({})

let theList = ref()
const onSubmit = (form, done) => {
	// 得到导入表格的数据
	loading.value = true
	theList.value.getData((data) => {
		let copyFrom = cloneDeep(form)
		copyFrom.bscTrainingCourseList = data
		if (copyFrom.signUpTime) {
			copyFrom.signUpStartTime = copyFrom.signUpTime[0]
			copyFrom.signUpEndTime = copyFrom.signUpTime[1]
			copyFrom.signUpTime = undefined
		}
		copyFrom.approvalStatus = '1'
		copyFrom.approvalStatusLabel = '待审批'
		copyFrom.id = id.value
		apiMod
			.view_bscTrainingManagement_update(copyFrom)
			.then((res) => {
				if (res.code == 0) {
					ElMessage.success('提交成功')
					loading.value = false
					emit('refresh')
					done()
				} else {
					loading.value = false
					emit('refresh')
					done()
				}
			})
			.catch(() => {
				done()
				loading.value = false
				// 错误不关闭弹窗刷新
				// emit('refresh')
			})
	})
}
let formRef = ref()
const toUpdate = () => {
	formRef.value.onSubmit()
}
</script>
<style scoped lang="scss">
.btn_part {
	margin-top: 40px;
	width: 100%;
	display: flex;
	flex-direction: row;
	justify-content: center;
}
</style>
